package com.suoluo.zenadmin.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.suoluo.zenadmin.common.CommonResp;
import com.suoluo.zenadmin.common.SystemType;
import com.suoluo.zenadmin.dao.InfoDAO;
import com.suoluo.zenadmin.oss.OSSService;
import com.suoluo.zenadmin.vo.BaseInfoMetadata;
import com.suoluo.zenadmin.vo.BaseInfoVO;
import com.suoluo.zenadmin.vo.ImageMetadata;
import com.suoluo.zenadmin.vo.ImageVO;

public class InfoServiceImpl implements InfoService {

	@Autowired
	private InfoDAO infoDAO;

	@Resource(name = "ossService")
	private OSSService ossService;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void saveBaseInfo(BaseInfoMetadata baseInfo) {
		if (baseInfo.getDataId() == 0 || baseInfo.getInfoType() == 0) {
			throw new IllegalArgumentException("data_id or info_type is null");
		}
		int infoId = infoDAO.addBaseInfo(baseInfo);
		baseInfo.setInfoId(infoId);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void saveImage(ImageMetadata image) {
		if (image.getInfoId() == 0) {
			throw new IllegalArgumentException("base info id is null");
		}
		String source = ossService.putImage(image.getImagepart());
		try {
			image.setSource(source);
			image.setSmall(SystemType.IMAGE_SMALL);
			image.setMiddle(SystemType.IMAGE_MIDDLE);
			image.setNormal(SystemType.IMAGE_NORMAL);
			int imageId = infoDAO.addImage(image);
			image.setImageId(imageId);
		} catch (Exception e) {
			ossService.deleteImage(source);
			throw new ImageException(CommonResp.FAIL_CODE, "保存图片到数据库失败", e,
					source);
		}
	}

	@Override
	public List<Integer> saveImage(int infoId, List<String> imageKeys) {

		if (infoId == 0) {
			throw new IllegalArgumentException("base info id is null");
		}
		if (imageKeys == null) {
			return Collections.emptyList();
		}
		List<Integer> imageIdList = new ArrayList<Integer>();
		for (String imageKey : imageKeys) {
			ImageVO image = new ImageVO();
			image.setInfoId(infoId);
			image.setSource(imageKey);
			image.setSmall(SystemType.IMAGE_SMALL);
			image.setMiddle(SystemType.IMAGE_MIDDLE);
			image.setNormal(SystemType.IMAGE_NORMAL);
			int imageId = infoDAO.addImage(image);
			imageIdList.add(imageId);
		}
		return imageIdList;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void updateBaseInfo(BaseInfoMetadata baseInfo) {
		if (baseInfo.getDataId() == 0 || baseInfo.getInfoType() == 0) {
			throw new IllegalArgumentException("data_id or info_type is null");
		}
		infoDAO.updateBaseInfo(baseInfo);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void updateImage(ImageMetadata image) {
		MultipartFile mfile = image.getImagepart();
		if (mfile != null && !mfile.isEmpty()) {
			String updateSource = ossService.updateImage(mfile,
					image.getSource());
			if (updateSource != null && !updateSource.equals(image.getSource())) {
				infoDAO.updateImage(image.getImageId(), updateSource);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void deleteBaseInfo(BaseInfoMetadata baseInfo) {
		List<BaseInfoVO> infos = infoDAO.findBaseInfo(baseInfo.getDataId(),
				baseInfo.getInfoType());
		if (infos == null || infos.isEmpty()) {
			return;
		}
		List<ImageVO> images = new ArrayList<ImageVO>();
		for (BaseInfoVO info : infos) {
			List<ImageVO> subImages = infoDAO.findImages(info.getInfoId());
			images.addAll(subImages);
		}
		infoDAO.deleteImages(infos);
		infoDAO.deleteBaseInfo(baseInfo.getDataId(), baseInfo.getInfoType());
		for (ImageVO image : images) {
			ossService.deleteImage(image.getSource());
		}
	}

	@Override
	public List<ImageVO> findImages(int infoId) {
		return infoDAO.findImages(infoId);
	}

	@Override
	public List<BaseInfoVO> findBaseInfos(int dataId, int infoType) {
		return infoDAO.findBaseInfo(dataId, infoType);
	}

	@Override
	public List<BaseInfoVO> findDefaultBaseInfos(int[] dataIds, int infoType) {
		return infoDAO.findDefaultBaseInfos(dataIds, infoType);
	}

	@Override
	public List<BaseInfoVO> findDefaultBaseInfos(List<BaseInfoVO> infos) {
		if (infos == null || infos.isEmpty()) {
			return Collections.emptyList();
		}
		StringBuilder condition = new StringBuilder("default_info=1 and (");
		for (BaseInfoVO info : infos) {
			condition.append("(data_id=").append(info.getDataId())
					.append(" and ").append("info_type=")
					.append(info.getInfoType()).append(")").append(" or");
		}
		// 将最后的一个or替换成右括号
		condition.replace(condition.length() - 2, condition.length(), ")");
		return infoDAO.findDefaultBaseInfos(condition.toString());
	}

	@Override
	public ImageVO findImageById(int imageId) {
		return infoDAO.findImageById(imageId);
	}

	@Override
	public List<BaseInfoVO> findBaseInfoByInfoId(int infoId) {
		return infoDAO.findBaseInfoByInfoId(infoId);
	}

	@Override
	public void deleteImage(ImageVO image) {
		// 删除数据
		infoDAO.deleteImage(image.getImageId());
		// 删除图片
		ossService.deleteImage(image.getSource());
	}

	@Override
	public void deleteBaseInfoById(int infoId) {

		List<BaseInfoVO> base = new ArrayList<BaseInfoVO>();
		BaseInfoVO baseInfo = new BaseInfoVO();
		baseInfo.setInfoId(infoId);
		base.add(baseInfo);
		// 删除图片数据
		infoDAO.deleteImages(base);
		// 删除图片
		List<ImageVO> subImages = infoDAO.findImages(infoId);
		for (ImageVO image : subImages) {
			ossService.deleteImage(image.getSource());
		}
		infoDAO.deleteBaseInfoById(infoId);
	}

	@Override
	public BaseInfoVO findBaseInfoById(int infoId) {
		return infoDAO.findBaseInfoById(infoId);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<ImageVO> findImages(int[] infoIds) {
		return infoDAO.findImages(infoIds);
	}

	@Override
	public List<BaseInfoVO> findBaseInfosNoDefault(int dataId, int infoType) {
		return infoDAO.findBaseInfoNoDefault(dataId,infoType);
	}
}
